define("pages/jls_sj/mainksrs", [
        "../../plugins/artTemplate/artTemplate",
        "../../common/tools",
        "../../common/componts",
        "../../common/kss",
    ],
    function(require) {
        var comonts=require("../../common/componts");
        var Select = function(){
            comonts.initSelectPage(undefined);
            comonts.initSelect(undefined,undefined,undefined);
        }
        var date = new Date();
        var str = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
        var str2 = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();

        var isBtx = function () {
            var xm = $("#xmfm").textbox("getValue");
            var xb = $("#xbfm").val();
            var jsh = $("#jshfm").textbox("getValue");
            var rsrq = $("#rsrqfm").datetimebox("getValue");
            var rsxz = $("#rsxzfm").val();
            var dah = $("#dahfm").textbox("getValue");
            console.log(jsh.length);
            if (xm == "" || xm == null){
                $.messager.alert("提示", '姓名不能为空！','info');
                return false;
            } else if (xb == "" || xb == null) {
                $.messager.alert("提示", '性别不能为空！','info');
                return false;
            }else if (jsh == "" || jsh == null){
                $.messager.alert("提示", '监室号不能为空！','info');
                return false;
            }else if (jsh.length>4){
                $.messager.alert("提示", '监室号最大长度为4！','info');
                return false;
            } else if (rsrq == "" || rsrq == null) {
                $.messager.alert("提示", '入所日期不能为空！','info');
                return false;
            }else if (rsxz == "" || rsxz == null) {
                $.messager.alert("提示", '入所原因不能为空！','info');
                return false;
            }else if (dah == "" || dah == null) {
                $.messager.alert("提示", '档案号不能为空！','info');
                return false;
            }else if (dah.length>20) {
                $.messager.alert("提示", '档案号最大长度为20！','info');
                return false;
            }
            return true;
        }

        var creator = $("#tbr").val();
        console.log("creator: "+creator)
        $("#creator").val(creator);
        $("#tbsjfm").val(str2);
        $("#zpcreator").val(creator);
        var save = function(){
            //调用验证身份证得方法
            var url = "/kss_xyr/saveForKsrs";
            var data =$("#ksrsJbxxForm").serialize();
            console.log(data);
            submitLoad();
            $.ajax({
                type : "POST",  //提交方式
                url : url,
                data : data,//数据，这里使用的是Json格式进行传输
                success : function(result) {
                    if (result.success) {
                        $.messager.alert("提示","保存成功","info",function () {
                            window.location.reload();
                        });

                    }else{
                        removeLoad();
                        $.messager.alert("提示","保存失败","info");
                    }
                },
                complete:function(){
                    removeLoad();
                },
            });
        }

        //保存照片信息
        var zpsave = function (gcbh) {
            var zmz = $('#upzmz')[0].files[0];
            console.log($('#upzmz'))
            console.log(zmz)
            var cmz1 = $('#upcmz1')[0].files[0];
            var cmz2 = $('#upcmz2')[0].files[0];
            var dataObj = $("#ksrsZpForm").serializeObject();
            var formData = JSON.stringify(dataObj);
            var sendData = new FormData();
            sendData.append('qm', zmz);
            sendData.append('zm', cmz1);
            sendData.append('ym', cmz2);
            sendData.append('gcbh', gcbh);
            sendData.append('formData', formData);
            //调用验证身份证得方法
            var url = "/kss_xyr/saveForKsrsZp";
            console.log("data: "+sendData);
            submitLoad();
            $.ajax({
                type : "POST",  //提交方式
                processData : false,
                contentType : false,
                dataType : "json",
                url : url,
                data : sendData,//数据，这里使用的是Json格式进行传输
                success : function(result) {
                    if (result.success) {
                        $.messager.alert("提示","保存成功","info",function () {
                            window.location.reload();
                        });

                    }else{
                        removeLoad();
                        $.messager.alert("提示","照片保存失败","info");
                    }
                },
                complete:function(){
                    removeLoad();
                },
            });
        }

        var bindAction=function(){
            $("#zrs_save").on("click",function(){
                if (isBtx() == true){
                    var qm = $('#upzmz')[0].files[0];
                    var zm = $('#upcmz1')[0].files[0];
                    var ym = $('#upcmz2')[0].files[0];
                    console.log("qm"+qm+",zm"+zm+",ym"+ym);
                    if ((qm != null && qm != undefined) || (zm != null && zm != undefined) || (ym != null && ym != undefined)) {
                        var url = "/kss_xyr/saveForKsrs";
                        var data =$("#ksrsJbxxForm").serialize();
                        console.log(data);
                        submitLoad();
                        $.ajax({
                            type : "POST",  //提交方式
                            url : url,
                            data : data,//数据，这里使用的是Json格式进行传输
                            success : function(result) {
                                if (result.success) {
                                    var gcbh = result.result.result;
                                    console.log("gcbh: "+gcbh);
                                    zpsave(gcbh)
                                }else{
                                    removeLoad();
                                    $.messager.alert("提示","基本信息保存失败","info");
                                }
                            },
                        });
                    } else {
                        $.messager.confirm("提示", "该人员未上传照片,是否保存？", function (b) {
                            if (b) {
                                save();
                            }
                        });
                    }
                }

            });
            $("#zp_save").on("click",function(){
                if (isBtx() == true){
                    var qm = $('#upzmz')[0].files[0];
                    var zm = $('#upcmz1')[0].files[0];
                    var ym = $('#upcmz2')[0].files[0];
                    if ((qm != null && qm != undefined) || (zm != null && zm != undefined) || (ym != null && ym != undefined)){
                    var url = "/kss_xyr/saveForKsrs";
                    var data =$("#ksrsJbxxForm").serialize();
                    console.log(data);
                    submitLoad();
                    $.ajax({
                        type : "POST",  //提交方式
                        url : url,
                        data : data,//数据，这里使用的是Json格式进行传输
                        success : function(result) {
                            if (result.success) {
                                var gcbh = result.result.result;
                                console.log("gcbh: "+gcbh);
                                zpsave(gcbh)
                            }else{
                                removeLoad();
                                $.messager.alert("提示","基本信息保存失败","info");
                            }
                        },
                    });
                }else{
                    $.messager.confirm("提示","该人员未上传照片,是否保存？",function (b) {
                        if (b){
                            save();
                        }
                    });
                }
                }
            });

            $(window).keydown(function(event) {
                if (event.keyCode == 13) {
                }
            });

        }
        var initView=function(){
            bindAction();
            Select();
        }
        initView();
    });
